using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ObjMove : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private RectTransform rectTransform;
    private Canvas canvas;
    private Vector2 offset;

    // Start is called before the first frame update
    void Start()
    {
        // 获取 RectTransform 组件
        rectTransform = GetComponent<RectTransform>();
        if (rectTransform == null)
        {
            Debug.LogError("物体 " + gameObject.name + " 缺少 RectTransform 组件！");
            return;
        }

        // 获取 Canvas 组件
        canvas = GetComponentInParent<Canvas>();
        if (canvas == null)
        {
            Debug.LogError("物体 " + gameObject.name + " 的父物体中没有找到 Canvas 组件！");
            return;
        }

        Debug.Log("ObjMove 初始化完成，物体名称: " + gameObject.name);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("开始拖拽: " + gameObject.name);
        offset = rectTransform.anchoredPosition - eventData.position;
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("拖拽中: " + gameObject.name + ", 鼠标位置: " + eventData.position);
        rectTransform.anchoredPosition = eventData.position + offset;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("结束拖拽: " + gameObject.name);
    }
}
